package com.migu.contacts.login;

import static com.migu.contacts.net.Service.API;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.util.AutoDeleteUtil;
import com.frank.libandroid.util.CountDownUtil;
import com.frank.libandroid.util.PasswordVisibleUtil;
import com.frank.libandroid.util.TextWatcherUtil;
import com.migu.contacts.BuildConfig;
import com.migu.contacts.bean.RegisterReq;
import com.migu.contacts.databinding.ActivityRegisterBinding;
import com.migu.contacts.main.my.PrivacyActivity;
import com.migu.contacts.main.my.UserProtocolActivity;

public class RegisterActivity extends BaseActivity<ActivityRegisterBinding> {
    public void goPrivacy(View view) {
        start(PrivacyActivity.class);
    }

    public void goProtocol(View view) {
        start(UserProtocolActivity.class);
    }


    public void getCode(View view) {
        Log.d(TAG, "getCode() called with: view = [" + view + "]");
        RegisterReq bean = mBinding.getBean();
        if (TextUtils.isEmpty(bean.mobile)) {
            ToastUtils.showShort("请输入手机号");
            return;
        }
        CountDownUtil.startCountDown(mBinding.verifyCode, 60);
        API.sendSms(1, bean.mobile).watch(this, new HttpCallback<Boolean>() {
            @Override
            protected void onSuccess(Boolean aBoolean) {
                Log.d(TAG, "onSuccess() called with: aBoolean = [" + aBoolean + "]");
                if (aBoolean) {
                    ToastUtils.showShort("发送成功");
                    if (BuildConfig.DEBUG) {
                        bean.code = "111111";
                        mBinding.setBean(bean);
                    }
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        RegisterReq registerReq = new RegisterReq();
        if (BuildConfig.DEBUG) {
            registerReq.mobile = "13938260436";
        }
        mBinding.setBean(registerReq);
    }

    @Override
    protected void initView() {
        AutoDeleteUtil.autoDelete(mBinding.phone, mBinding.phoneDelete);
        TextWatcherUtil.setTextWatchEnable(mBinding.btn, mBinding.phone, mBinding.pwdInput, mBinding.codeInput);
        PasswordVisibleUtil.setPasswordVisible(mBinding.pwdInput, mBinding.showPwd);
//        CountDownUtil.setCountDown(mBinding.verifyCode, 60);

    }

    public void switchStatus(View view) {
        view.setSelected(!view.isSelected());
    }

    public void back(View view) {
        finish();
    }


    public void phoneLogin(View view) {
        start(LoginActivity.class);
    }

    public void login(View view) {
        if (!mBinding.check.isSelected()) {
            ToastUtils.showShort("请阅读平台协议");
            return;
        }
        API.register(mBinding.getBean()).watch(this, new HttpCallback<Boolean>() {
            @Override
            protected void onSuccess(Boolean aBoolean) {
                ToastUtils.showShort("注册成功");
                if (aBoolean) {
                    finish();
                    phoneLogin(null);
                }
            }
        });
    }
}